package org.zebra.web.annotation.sign.impl;

import cn.hutool.core.util.ObjUtil;
import org.zebra.common.util.SignUtil;
import org.zebra.web.annotation.sign.CheckSignStrategy;

import java.util.Map;

/**
 * @author zhanghongbin
 */
public class DefaultCheckSignStrategy implements CheckSignStrategy {

    private String secretKey;

    public DefaultCheckSignStrategy(String secretKey) {
        this.secretKey = secretKey;
    }

    @Override
    public boolean check(Map<String, Object> paramMap) {
        // 获取必须的三个参数
        Object timestampValue = paramMap.get(SignUtil.TIMESTAMP);
        Object nonceValue = paramMap.get(SignUtil.NONCE);
        Object signValue = paramMap.get(SignUtil.SIGN);
        if (ObjUtil.isEmpty(timestampValue) || ObjUtil.isEmpty(nonceValue) || ObjUtil.isEmpty(signValue)) {
            return false;
        }
        String theSign = SignUtil.createSign(this.secretKey, paramMap);
        return theSign.equals(signValue);
    }
}
